package com.jcli.novel_t.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;

public class TextFileProcessorUI02 extends JFrame {
    private JTextArea textArea;
    private JButton saveButton;
    private JButton convertButton;
    private JTextArea resultTextArea;

    public TextFileProcessorUI02() {
        setTitle("Text File Processor");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create components
        textArea = new JTextArea();
        saveButton = new JButton("Save");
        convertButton = new JButton("Start Conversion");
        resultTextArea = new JTextArea();

        // Create panels
        JPanel westPanel = new JPanel();
        westPanel.setLayout(new BorderLayout());
        westPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
        westPanel.add(saveButton, BorderLayout.SOUTH);

        JPanel eastPanel = new JPanel();
        eastPanel.setLayout(new BorderLayout());
        eastPanel.add(new JScrollPane(resultTextArea), BorderLayout.CENTER);
        eastPanel.add(convertButton, BorderLayout.SOUTH);

        // Add panels to the frame
        getContentPane().setLayout(new GridLayout(1, 2));
        getContentPane().add(westPanel);
        getContentPane().add(eastPanel);

        // Load initial content from input.txt
        loadInitialContent();

        // Add action listeners
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveToFile();
            }
        });

        convertButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startConversion();
            }
        });
    }

    private void loadInitialContent() {
        try {
            String content = new String(Files.readAllBytes(Paths.get("input.txt")));
            textArea.setText(content);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Error loading initial content: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void saveToFile() {
        try {
            String content = textArea.getText();
            Files.write(Paths.get("input.txt"), content.getBytes());
            JOptionPane.showMessageDialog(this, "File saved successfully!");
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Error saving file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void startConversion() {
        // Replace this with your code to perform file conversion
        // You can use the content of textArea and save the result in resultTextArea
        String inputText = textArea.getText();
        // Perform your conversion logic here and set the result in resultTextArea
        String convertedText = performConversion(inputText);
        resultTextArea.setText(convertedText);
        // Save the converted text to a file
        try {
            Files.write(Paths.get("output.txt"), convertedText.getBytes());
            JOptionPane.showMessageDialog(this, "Conversion completed and saved to 'output.txt'!");
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Error saving converted file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

    }
    private String performConversion(String inputText) {
        // Replace this with your actual conversion logic
        // For example, replace "abc" with "xyz"
        return inputText.replaceAll("abc", "xyz");
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TextFileProcessorUI02 ui = new TextFileProcessorUI02();
                ui.setVisible(true);
            }
        });
    }
}
